import Taro from "@tarojs/taro";

const baseUrl = "http://localhost:3000/api";

export default ({ url, data, method = "get" }) => {
  return new Promise((reslove, reject) => {
    Taro.request({
      url: baseUrl + url,
      data,
      method: method as keyof Taro.request.Method,
      timeout: 30000,
      header: {
        Authorization: Taro.getStorageSync("Authorization"),
      },
      success: (res) => {
        const { data } = res;

        if (data.status !== 200) {
          Taro.showToast({
            icon: "error",
            title: data.message,
          });
          reject(data);
          return;
        }

        reslove(res.data);
      },
      fail: (res) => {
        Taro.showToast({
          icon: "error",
          title: res.errMsg,
        });
        reject(res);
      },
    });
  });
};
